package com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TCargo;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TEntryInfo;
import com.zz.ecommerce.togatherDeclareMgr.common.dao.TCargoDao;
import com.zz.ecommerce.togatherDeclareMgr.common.dao.TEntryInfoDao;
import com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload.service.CargoCustService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 报关单上传
 * Created by admin on 2016/1/14.
 */
@Controller
@RequestMapping("/togatherDeclareMgr/custDeclarationUpload")
public class CustDeclarationUploadController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CustDeclarationUploadController.class);

    @Autowired
    private TEntryInfoDao entryInfoDao;

    @Autowired
    private TCargoDao cargoDao;

    @Autowired
    private CargoCustService cargoCustService;

    @RequestMapping("/init")
    public String init()
    {
        return "/togatherDeclareMgr/custDeclarationUpload/index";
    }

    /**
     * 查询申报单列表
     *
     * @param head
     * @param request
     * @return
     */
    @RequestMapping("/queryDeclareList")
    @ResponseBody
    public GridResult queryDeclareList(TEntryInfo head, GridRequest request) {
        int count = entryInfoDao.count("T_ENTRYINFO", head);
        List<TEntryInfo> headList = entryInfoDao.page("T_ENTRYINFO", head,
                request.getStart(), request.getEnd(), request.getSortOrder());

        GridResult result = new GridResult(headList, request, count);
        return result;
    }

    @RequestMapping("/edit")
    public String edit(TEntryInfo head, ModelMap modelMap)
    {
        head = entryInfoDao.query("T_ENTRYINFO", head);
        modelMap.put("head", head);

        if("E".equals(head.getEntryty()))
        {
            return "/togatherDeclareMgr/custDeclarationUpload/exportCust/edit";
        }

        throw new RuntimeException("只支持出口报关单!");
    }

    @RequestMapping("/editHead")
    public String editHead(TEntryInfo head, ModelMap modelMap)
    {
        head = entryInfoDao.query("T_ENTRYINFO", head);
        modelMap.put("head", head);

        return "/togatherDeclareMgr/custDeclarationUpload/exportCust/edit_header";
    }

    @RequestMapping("/editCargo")
    public String editCargo()
    {
        return "/togatherDeclareMgr/custDeclarationUpload/exportCust/edit_cargo";
    }

    @RequestMapping(value = "/pageCargo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult pageCargo(GridRequest gridRequest, TCargo cargo) {

        List<TCargo> list = cargoDao.page("T_CARGO", cargo, gridRequest.getStart(), gridRequest.getEnd(), gridRequest.getSortOrder());
        long count = cargoDao.count("T_CARGO", cargo);
        return new GridResult(list, gridRequest, count);
    }

    @RequestMapping(value = "/loadCargo", method = {RequestMethod.GET, RequestMethod.POST})
    public String loadCargo(TCargo cargo, ModelMap modelMap) {
        if (StringUtils.isNotEmpty(cargo.getIndx())) {
            modelMap.addAttribute("cargo", cargoDao.query(TableConstants.T_CARGO, cargo));
        }
        return "/togatherDeclareMgr/custDeclarationUpload/exportCust/load_cargo";
    }

    @RequestMapping("/saveCargo")
    @ResponseBody
    public void saveCargo(TCargo cargo)
    {
        cargoCustService.saveCargo(cargo);
    }

    @RequestMapping(value = "/deleteCargo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String deleteCargo(@RequestParam("index")String indx) {

        TCargo cargo = new TCargo();
        cargo.setIndx(indx);

        cargoCustService.deleteCargoInfo(cargo);

        return "success";
    }

    @RequestMapping(value = "/statCargo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map statCargo(TCargo cargo) {
        return cargoCustService.statCargo(cargo);
    }

    @RequestMapping("/save")
    @ResponseBody
    public void save(TEntryInfo head)
    {
        head.setEntryid(head.getPreentryid());
        entryInfoDao.update("T_ENTRYINFO", head, " where indx=#{updateObj.indx}");
    }

}
